package cn.xinfei.xdecision.data.factory;

import cn.xinfei.xdecision.data.RiskDataApiInterface;
import cn.xinfei.xdecision.model.*;
import com.alibaba.fastjson.JSONObject;
import com.dianping.cat.Cat;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import cn.xinfei.xdecision.common.utils.util.JsonUtil;
@Slf4j
@Service
public class RiskDApiFallbackFactory implements FallbackFactory<RiskDataApiInterface> {


    @Override
    public RiskDataApiInterface create(Throwable throwable) {
        return new RiskDataApiInterface() {



            @Override
            public VariableApiResponse preCollect(VariableApiRequest variableApiRequest) {
                log.error("preCollect接口进入熔断回调函数，apiRequest={}", JsonUtil.toJson(variableApiRequest),throwable);
                Cat.logError(throwable);
                long start = System.currentTimeMillis();
                VariableApiResponse response = new VariableApiResponse();
                try {
                    Map<String, Object> fieldMap = new HashMap<>();
                    fieldMap.put("aswer",22);
                    fieldMap.put("age",22);
                    fieldMap.put("sex",1);
                    fieldMap.put("phone","18710273386");
                    fieldMap.put("name","shiyusen");
                    fieldMap.put("idcard","37292519910270000");
                    fieldMap.put("idCard","37292519910270000");
                    long end = System.currentTimeMillis();
                    response.setVariablesResponse(JsonUtil.toJson(fieldMap));
                    log.info("============ 接口mock调用耗时：{},fieldMap={}", (end - start), fieldMap);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return response;
            }

            @Override
            public VariableApiResponse realCollect(VariableApiRequest variableApiRequest) {
                log.error("realCollect接口进入熔断回调函数，apiRequest={}", JsonUtil.toJson(variableApiRequest),throwable);
                Cat.logError(throwable);
                long start = System.currentTimeMillis();
                VariableApiResponse response = new VariableApiResponse();
                try {
                    Map<String, Object> fieldMap = new HashMap<>();
                    fieldMap.put("aswer",22);
                    fieldMap.put("age",22);
                    fieldMap.put("sex",1);
                    fieldMap.put("phone","18710273386");
                    fieldMap.put("name","shiyusen");
                    fieldMap.put("idcard","37292519910270000");
                    fieldMap.put("idCard","37292519910270000");
                    long end = System.currentTimeMillis();
                    response.setVariablesResponse(JsonUtil.toJson(fieldMap));
                    log.info("============ 接口mock调用耗时：{},fieldMap={}", (end - start), fieldMap);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return response;
            }

//            @Override
//            public CreditVarResponse creditCollect(CreditVarRequest creditVarRequest) {
//                log.error("creditCollect接口进入熔断回调函数，apiRequest={}", JsonUtil.toJson(creditVarRequest),throwable);
//                Cat.logError(throwable);
//                long start = System.currentTimeMillis();
//                CreditVarResponse response = new CreditVarResponse();
//                try {
//                    Map<String, Object> fieldMap = new HashMap<>();
//                    fieldMap.put("aswer",22);
//                    fieldMap.put("age",22);
//                    fieldMap.put("sex",1);
//                    fieldMap.put("phone","18710273386");
//                    fieldMap.put("name","shiyusen");
//                    fieldMap.put("idcard","37292519910270000");
//                    fieldMap.put("idCard","37292519910270000");
//                    long end = System.currentTimeMillis();
//                    response.setData(JsonUtil.toJson(fieldMap));
//                    log.info("============ 接口mock调用耗时：{},fieldMap={}", (end - start), fieldMap);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//
//                return response;
//            }
//
//            @Override
//            public VariableApiResponse fetchCreditVars(VariableApiRequest variableApiRequest) {
//                log.error("fetchCreditVars，apiRequest={}", JsonUtil.toJson(variableApiRequest),throwable);
//                Cat.logError(throwable);
//                long start = System.currentTimeMillis();
//                VariableApiResponse response = new VariableApiResponse();
//                try {
//                    Map<String, Object> fieldMap = new HashMap<>();
//                    fieldMap.put("aswer",22);
//                    fieldMap.put("age",22);
//                    fieldMap.put("sex",1);
//                    fieldMap.put("phone","18710273386");
//                    fieldMap.put("name","shiyusen");
//                    fieldMap.put("idcard","37292519910270000");
//                    fieldMap.put("idCard","37292519910270000");
//                    long end = System.currentTimeMillis();
//                    response.setVariablesResponse(JsonUtil.toJson(fieldMap));
//                    log.info("============ 接口mock调用耗时：{},fieldMap={}", (end - start), fieldMap);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//
//                return response;
//            }
        };

    }
}